package com.hopedove.workshop.ui.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.hopedove.workshop.R
import com.hopedove.workshop.data.pojo.ExchangeBatchItem
import com.hopedove.workshop.databinding.WorkshopBatchListItemBinding

class ExchangeBatchConfirmAdapter : RecyclerView.Adapter<ExchangeBatchConfirmAdapter.VH>() {

    var data: List<ExchangeBatchItem> = mutableListOf()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
        val binding: WorkshopBatchListItemBinding = DataBindingUtil.inflate(
            LayoutInflater.from(parent.context),
            R.layout.workshop_batch_list_item,
            parent,
            false
        )

        return VH(binding)
    }

    override fun onBindViewHolder(holder: VH, position: Int) {
        holder.render(data, position)
    }

    override fun getItemCount(): Int = data.size

    fun updateDataSet(list: List<ExchangeBatchItem>) {
        data = list
        notifyDataSetChanged()
    }

    class VH(val binding: WorkshopBatchListItemBinding) : RecyclerView.ViewHolder(binding.root) {

        fun render(data: List<ExchangeBatchItem>?, position: Int) {
            data?.let {
                val item = it[position]
                binding.item = item
            }
        }

    }

}